﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Ex6.RecursivelyCopyDir
{
    class RecursivelyCopyDir
    {
        static void Main(string[] args)
        {
            string dirName = "..\\..\\TestFolder";
            string newDirName = "..\\..\\NewFolder";
            Directory.CreateDirectory(newDirName);
            Traverse(dirName, newDirName);
        }

        private static void Traverse(string dirName, string newDirName)
        {
            try
            {
                string[] files = Directory.GetFiles(dirName);

                foreach (var file in files)
                {
                    string newDir = file.ToString();
                    newDir = newDir.Replace(dirName,newDirName);
                    File.Copy(file,newDir);
                }

                string[] subdirs = Directory.GetDirectories(dirName);
                foreach (string subdir in subdirs)
                {
                    string newDir = subdir.ToString();
                    newDir = newDir.Replace(dirName, newDirName);
                    Directory.CreateDirectory(newDir);
                    Traverse(subdir, newDir);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Can not access directory: {0}", dirName);
            }
        }
    }
}
